/*
 This file is part of Musimage, a project which makes possible listen to music
 and, at the same time, watch pictures related with that music.
 Copyright (C) 2006  David Garcia Narbona 

 Musimage is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 Musimage is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with Musimage; if not, write to the Free Software
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
package client;

import java.io.*;
import java.util.Vector;

// Clase que parsea una lista de canciones MP3, es decir, un archivo con
// con extension m3u, y devuelve el conjunto de canciones de la lista
// para ser reproducidas en un formato adecuado para la aplicacion

// NOTA: el codigo de esta clase es parte del codigo de la clase Playlist del
// package
// helliker.id3 de la aplicacion jd3Lib de: (c) 2001 Jonathan Hilliker.
public class LoadMP3List extends Vector {

	// **Atributos **/////////////////////////////////////////
	private boolean flag;

	private LoadMP3ListException loadMP3ListException;

	// ///////////////////////////////////////////////////////

	// ** Metodos **/////////////////////////////////////////

	// Constructor
	public LoadMP3List() {

		flag = true;
		loadMP3ListException = new LoadMP3ListException();
	}

	public Vector loadFromFile(File m3uFile) throws LoadMP3ListException,
			FileNotFoundException, IOException {

		FileReader in = new FileReader(m3uFile);
		BufferedReader reader = new BufferedReader(in);
		String str = reader.readLine();

		if (this != null)
			this.clear();

		if (!str.equals("#EXTM3U\r\n")) {

			while ((str = reader.readLine()) != null) {

				if (str.indexOf("EXTINF:") != -1) {

					str = reader.readLine();

					try {
						if (str.indexOf(":") == -1)
							this.add(str);
					} catch (Exception e) {
						// Do nothing, just don't add it
					}
				} else {
					throw loadMP3ListException;
				}
			}
		} else {
			throw loadMP3ListException;
		}

		reader.close();
		in.close();
		return this;
	}
}
